import Vue from 'vue';
import Vuex from 'vuex';
import request from "@/api/request";

Vue.use(Vuex)

//存放数据
const state = {
    token:"",//登录成功返回的token
    user:{},//用户信息
}

//读取数据
const getters = {
    getToken: () => sessionStorage.getItem('token'),//从浏览器获取token信息
}
//操作数据
const mutations = {
    setToken(state,value){//设置token
        state.token = value;
        sessionStorage.setItem("token",value)
    },
    setUser(state,value){//存储用户信息
        state.user = value
    }
}

//响应组件动作
const actions = {
    //    获取用户数据
    async getUser(context){
        let res = await request.loginOrRegister.getBasicInfo()
        if (res.code === 200){
            context.commit('setUser',res.data)
            return res.data
        }
    }
}

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
})
